#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<string.h>
#include<signal.h>
#include"../h/test_manage_def.h"
#include"../../../common/h/logging.h"
#include <libxml/xmlreader.h>

tm_config_info_t g_config_info;

U8 testm_reading_configure(U8 *filename);
U8 testm_sender_handler();
U8 testm_receiver_handler();
U8 init_test_management();
U8 testm_create_client_handler();

U8 init_test_management()
{
	U8 index;
	for(index=0;index<TESTM_MAXIMUM_CLIENT;index++)
	{
		g_config_info.client_list[index].client_id = TESTM_INACTIVE_CLIENT_STATE;
	}
	testm_reading_configure(TESTM_FILE_CONFIGURATION);
}

U8 testm_sender_handler(tm_client_info_t *client_info)
{
	U32 index;
	U32 val_result = SYSLOG_FAILURE;
	for(index = 0;index < g_config_info.client_list[index].amount ;index ++)
	{
		/*send message*/
		write(client_info->socket_id,client_info->content,sizeof(client_info->content));

		SYSLOG_COMMON_INFO_LOGGING("Socket[%d] Send Message: %s",client_info->socket_id,\
				client_info->content);
		//SYSLOG_COMMON_INFO_LOGGING("Result: %d",val_result);
	}
	while(1){}
}

U8 testm_receiver_handler(tm_client_info_t *client_info)
{
	U8 content[100];
	while(SYSLOG_FAILURE !=read(client_info->socket_id,content,sizeof(content)+1))
	{
		printf("Receive : %s\n",content);
	}
}

U8 testm_create_client_handler()
{
	U8 index;
	U32 socket_client;
	pthread_t thread_key_snd = SYSLOG_NULL;
	pthread_t thread_key_rcv;
	void *result;
	struct sockaddr_in servername;
	for(index = 0;index < TESTM_MAXIMUM_CLIENT;index++)
	{
		if(TESTM_INACTIVE_CLIENT_STATE != g_config_info.client_list[index].client_id)
		{
			signal (SIGCHLD, SIG_IGN);
			socket_client = socket(AF_INET,SOCK_STREAM,0);
			if(SYSLOG_FAILURE == socket_client){
				printf("cannot create socket at client \n");
				return SYSLOG_FALSE;
			}
			printf("create socket [%d]\n",socket_client);

			servername.sin_family = AF_INET;
			servername.sin_port = htons(g_config_info.client_list[index].port);
			servername.sin_addr.s_addr = inet_addr(g_config_info.client_list[index].ip_addr);

			/*Connect to server*/
			if(connect(socket_client,(struct sockaddr*)&servername, sizeof(struct sockaddr)) < SYSLOG_SUCCESS)
			{
				printf("cannot connect to server \n");
				return SYSLOG_FALSE;
			}
			g_config_info.client_list[index].socket_id = socket_client;

			pthread_create(&thread_key_snd,SYSLOG_NULL,(void*)testm_sender_handler \
					,(void*)&(g_config_info.client_list[index]));

		}
		/*
		 *
		 pthread_create(&thread_key_rcv,SYSLOG_NULL,(void*)testm_receiver_handler \
				,(void*)&(g_config_info.client_list[index]));
		*/

	}
	if(SYSLOG_NULL != thread_key_snd)
	pthread_join(thread_key_snd,&result);


	//pthread_join(thread_key_rcv,&result);

}

U8 testm_reading_configure(U8 *filename)
{
    U8 test;
    U8 *xml_element;
    U8 *temp;
    U32 i;
    U8 client_number = TESTM_INACTIVE_CLIENT_STATE;

    xmlTextReaderPtr reader;

    reader = xmlReaderForFile(filename, NULL, 0);

    if(SYSLOG_NULL == reader)
    {
    	printf("Cann't read file\n");
    }

    while(xmlTextReaderRead(reader))
    {
        switch(xmlTextReaderNodeType(reader))
        {
        	case XML_READER_TYPE_ELEMENT:
                xml_element = (U8 *)xmlTextReaderConstName(reader);
                xmlTextReaderMoveToElement(reader);

                if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_TRANPORT_TEST_CLIENT))
                	client_number++;
                break;

        	case XML_READER_TYPE_TEXT:
        		temp = (U8 *)xmlTextReaderConstValue(reader);
        		if(SYSLOG_NULL == xml_element)
        		{
        			break;
        		}
        		if(SYSLOG_SUCCESS != strcmp(xml_element, XML_ELEMENT_TRANSPORT_TEST))
        		{
					if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_TRANPORT_TEST_CLIENT))
					{
						g_config_info.client_list[client_number].client_id = client_number;
						printf("client_id: %d\n", g_config_info.client_list[client_number].client_id);
					}
					else if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_TRANPORT_TEST_PORT))
					{
						g_config_info.client_list[client_number].port = atoi(temp);
						printf("Port: %d\n", g_config_info.client_list[client_number].port);
					}
					else if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_TRANSPORT_TEST_IP_ADDRESS))
					{
						g_config_info.client_list[client_number].client_id = TESTM_ACTIVE_CLIENT_STATE;
						strcpy(g_config_info.client_list[client_number].ip_addr,temp);
						printf("ip_addr: %s\n", g_config_info.client_list[client_number].ip_addr);
					}
					else if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_TRANSPORT_TEST_CONTENT))
					{
						strcpy(g_config_info.client_list[client_number].content,temp);
						printf("Content: %s\n", g_config_info.client_list[client_number].content);
					}
					else if(SYSLOG_SUCCESS==strcmp(xml_element, XML_ELEMENT_TRANSPORT_TEST_AMOUNT_PACKET))
					{
						g_config_info.client_list[client_number].amount = (U32)temp;
						printf("amount: %s\n", g_config_info.client_list[client_number].amount);
					}else{
						*xml_element = SYSLOG_NULL;
					}
        		}
        		break;

        	default:
        		//printf("xmlTextReaderNodeType: %d\n",xmlTextReaderNodeType(reader));
        		break;
        		/*
            case XML_READER_TYPE_ATTRIBUTE:
            	temp = (char *)xmlTextReaderConstValue(reader);
            	printf("Attribute: %d\n", atoi(temp));
            	if(!strcmp(temp, XML_ATTRIBUTE_TRANSPORT_TEST))
            		g_config_info.amount_client = atoi(temp);
            	else
            		xml_element = SYSLOG_NULL;
            	break;
            	*/
        }
    }

    xmlFreeTextReader(reader);
    xmlCleanupParser();

    return 0;
}

U32 main(U32 argc,U32 *argv)
{
	init_test_management();
	testm_create_client_handler();
	exit(SYSLOG_SUCCESS);
}
